﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Wpf_Examples
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void GetTxtPathData(object sender, RoutedEventArgs e)
        {
            try
            {
                string pathData = GetTextPath("XX科技公司", "楷体", 42);

                string pathData1 = GetTextPath("CSDN技术社区", "微软雅黑", 42);

                string pathData2 = GetTextPath("淘宝店铺", "华文行楷", 42);

                string pathData3 = GetTextPath("小五资源", "隶书", 42);

                // 你可以在这里做更多事情，比如显示路径数据或保存到文件
            }
            catch (Exception ex)
            {
                // 处理异常，例如记录日志或通知用户
                MessageBox.Show($"发生错误: {ex.Message}");
            }
        }

        public string GetTextPath(string word, string fontFamily, int fontSize)
        {
            if (string.IsNullOrEmpty(word))
                throw new ArgumentException("文本不能为空", nameof(word));
            if (fontSize <= 0)
                throw new ArgumentOutOfRangeException(nameof(fontSize), "字体大小必须大于0");

            try
            {
                Typeface typeface = new Typeface(new FontFamily(fontFamily), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
                return GetTextPathInternal(word, typeface, fontSize);
            }
            catch (ArgumentException ex)
            {
                throw new ArgumentException($"无效的字体名称: {fontFamily}", nameof(fontFamily), ex);
            }
        }
        public string GetTextPathInternal(string word, Typeface typeface, int fontSize)
        {
            FormattedText text = new FormattedText(word,new System.Globalization.CultureInfo("zh-cn"),FlowDirection.LeftToRight, typeface, fontSize,Brushes.Black); Geometry geo = text.BuildGeometry(new Point(0, 0));
            PathGeometry path = geo.GetFlattenedPathGeometry(); return path.ToString();
        }
    }
}
